﻿using System;
using System.Data;
using Nortec.Security;
using Litografia.BusinessEntities;
using System.Collections;
using System.Text;

namespace Litografia.DataObjects
{
    public class DOOTReportes : DOBase
    {
        #region Declarations
        private const string SearchOTEntregaSP = "OTEntregaSearch";
        private const string GetOTDetalleSP = "OTDetalleRep";
        //private const string GET_REGISTROOT_RESUMEN = "RegistroOTResumenRep";
        private const string GetOTResumenMultipleSP = "OTResumenMultipleRep";

        #endregion

        #region Methods: Public

        public static BEOTEntregaSearch.EntregaDataTable SearchOTEntrega(string number, int? status, DateTime? beginDate, DateTime? endDate, int companyId)
        {
            var result = new BEOTEntregaSearch();
            var command = DB.GetStoredProcCommand(SearchOTEntregaSP);
            DB.AddInParameter(command, "@NumeroOT", DbType.String, string.IsNullOrEmpty(number) ? Convert.DBNull : number);
            DB.AddInParameter(command, "@EstadoId", DbType.String, status.HasValue ? status.Value : Convert.DBNull);
            DB.AddInParameter(command, "@FechaInicial", DbType.DateTime, beginDate.HasValue ? beginDate.Value : Convert.DBNull);
            DB.AddInParameter(command, "@FechaFinal", DbType.DateTime, endDate.HasValue ? endDate.Value : Convert.DBNull);
            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, companyId);

            var dataset = DB.ExecuteDataSet(command);
            if (dataset != null && dataset.Tables.Count > 0)
            {
                dataset.Tables[0].TableName = result.Tables[0].TableName;
                result.Merge(dataset);
                result.EnforceConstraints = true;
            }
            return result.Entrega;
        }

        public static BEOTDetalleReporte OTDetalleReporteGet(int entregaId)
        {
            var command = DB.GetStoredProcCommand(GetOTDetalleSP);
            DB.AddInParameter(command, "@EntregaId", DbType.Int32, entregaId);

            var dataset = DB.ExecuteDataSet(command);
            var entity = new BEOTDetalleReporte();
            ConvertRawToEntity(dataset, entity);

            return entity;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "ex"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static BEOTResumenReporte RegistroOTResumenReporteGet(string number, string status, DateTime? beginDate, DateTime? endDate)
        {
            var command = DB.GetStoredProcCommand(GetOTResumenMultipleSP);
            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@NumeroOT", DbType.String, !String.IsNullOrEmpty(number) ? number : Convert.DBNull);
            DB.AddInParameter(command, "@EstadoId", DbType.String, !String.IsNullOrEmpty(status) ? status : Convert.DBNull);
            DB.AddInParameter(command, "@FechaInicial", DbType.DateTime, beginDate.HasValue ? beginDate.Value : Convert.DBNull);
            DB.AddInParameter(command, "@FechaFinal", DbType.DateTime, endDate.HasValue ? endDate.Value : Convert.DBNull);

            var dataset = DB.ExecuteDataSet(command);
            var entity = new BEOTResumenReporte();

            // Pasa informacion hacia al BE
            if (dataset != null && dataset.Tables.Count == 1)
            {
                dataset.Tables[0].TableName = entity.Tables[0].TableName;

                entity.Merge(dataset);
                entity.EnforceConstraints = true;
                
                //try
                //{
                //    entity.Merge(dataset);
                //    entity.EnforceConstraints = true;
                //}
                //catch (Exception ex)
                //{
                //    //
                //    //entity.HasErrors
                //}
            }
            return entity;
        }

        public static BEOTResumenMultipleReporte RegistroOTResumenReporteGet(ArrayList ordenTrabajoNumbers)
        {
            var result = new BEOTResumenMultipleReporte();
            var command = DB.GetStoredProcCommand(GetOTResumenMultipleSP);
            var numeroOTs = ArrayToString(ordenTrabajoNumbers);

            DB.AddInParameter(command, "@EmpresaId", DbType.Int32, NortecMembership.CompanyId);
            DB.AddInParameter(command, "@NumeroOT", DbType.String, numeroOTs);

            var dataset = DB.ExecuteDataSet(command);

            // Pasa informacion hacia el BE
            if (dataset != null && dataset.Tables.Count > 0)
            {
                dataset.Tables[0].TableName = result.Tables[0].TableName;
                result.Merge(dataset);
                result.EnforceConstraints = true;
            }
            return result;
        }

        #endregion

        #region Methods Non-Public

        private static string ArrayToString(IList array)
        {
            var sb = new StringBuilder();
            for (var i = 0; i < array.Count; i++)
            {
                if (i > 0)
                    sb.Append(",");

                sb.Append("'");
                sb.Append((string)array[i]);
                sb.Append("'");
            }
            return sb.ToString();
        }

        private static void ConvertRawToEntity(DataSet rawDataSet, DataSet entity)
        {
            entity.EnforceConstraints = false;

            for (int i = 1; i < rawDataSet.Tables.Count; i++)
                rawDataSet.Tables[i].TableName = rawDataSet.Tables[0].Rows[i]["TableName"].ToString();

            rawDataSet.Tables.Remove(rawDataSet.Tables[0]);

            entity.Merge(rawDataSet);
        }

        #endregion
    }
}
